package com.tanhua.recommend.listener;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.MovementScore;
import org.bson.types.ObjectId;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class MovementListener {

    @Autowired
    private MongoTemplate mongoTemplate;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    name = "tanhua.recommend.movement",
                    durable = "true"
            ),
            exchange = @Exchange(
                    name = "tanhua.log.exchange",
                    type = ExchangeTypes.TOPIC
            ),
            key = "log.movement"
    ))
    public void movementMessage(String message) {
        //1 解析消息内容
        JSONObject jsonObject = JSON.parseObject(message);

        //根据 动态id查询动态数据
        String movementId = jsonObject.getString("busId");
        Movement movement = mongoTemplate.findById(new ObjectId(movementId), Movement.class);

        //获取用户id
        Long userId = jsonObject.getLong("userId");

        //获取用户操作行为
        String type = jsonObject.getString("type");

        if (movement != null) {
            //2 封装动态推荐基础数据对象
            MovementScore score = new MovementScore();
            score.setMovementId(movement.getPid());//动态中，给推荐系统使用的唯一标识符  pid
            score.setUserId(userId);
            score.setDate(System.currentTimeMillis());
            score.setScore(score(type, movement)); //根据 用户的行为 和 动态数据 计算评分

            //3 保存数据
            mongoTemplate.save(score);
        }
    }

    //计算评分
    private Double score(String type, Movement movement) {
        //0201为发动态，  基础 5分   文字长度：50以内1分，50~100之间2分，100以上3分  每个图片1分
        //0202为浏览动态，  +1
        //0203为动态点赞，  +5
        //0204为动态喜欢，  +8
        //0205为评论，     +10
        //0206为动态取消点赞， -5
        //0207为动态取消喜欢， -8
        Double score = 0d;

        switch (type) {
            case ("0201"):
                score = 5d;  //基础分

                //文件长度评分 文字长度：50以内1分，50~100之间2分，100以上3分
                int length = StrUtil.length(movement.getTextContent());
                if (length < 50) {
                    score += 1d;
                } else if (length >= 50 && length < 100) {
                    score += 2d;
                } else {
                    score += 3d;
                }

                //每个图片1分
                score += movement.getMedias().size();

                break;
            case ("0202"):
                score = 1d;
                break;
            case ("0203"):
                score = 5d;
                break;
            case ("0204"):
                score = 8d;
                break;
            case ("0205"):
                score = 10d;
                break;
            case ("0206"):
                score = -5d;
                break;
            case ("0207"):
                score = -8d;
                break;
        }
        return score;
    }
}
